import service from './Request'
import util from './Util'
import store from '@/store';

/**
 * 异步get请求
 * @param url
 * @param result
 * @returns {Promise<unknown>}
 */
export const get = function(url, result) {
  return new Promise(resolve => {
    execute({
      url: url,
      method: 'get'
    }, result).then(res => {
      resolve(res)
    }).catch(res => {
      resolve(res)
    })
  })
}

/**
 * 异步post请求
 * @param url
 * @param body
 * @param result
 * @returns {Promise<unknown>}
 */
export const post = function(url, body, result) {
  return new Promise(resolve => {
    execute({
      url: url,
      method: 'post',
      data: body
    }, result).then(res => {
      resolve(res)
    }).catch(res => {
      resolve(res)
    })
  })
}

/**
 * 异步put请求
 * @param url
 * @param body
 * @param result
 * @returns {Promise<unknown>}
 */
export const put = function(url, body, result) {
  return new Promise(resolve => {
    execute({
      url: url,
      method: 'put',
      data: body
    }, result).then(res => {
      resolve(res)
    }).catch(res => {
      resolve(res)
    })
  })
}

/**
 * 异步delete请求
 * @param url
 * @param result
 * @returns {Promise<unknown>}
 */
export const exDelete = function(url, result) {
  return new Promise(resolve => {
    execute({
      url: url,
      method: 'delete'
    }, result).then(res => {
      resolve(res)
    }).catch(res => {
      resolve(res)
    })
  })
}
/**
 * 异步deletet提示请求
 * @param e
 * @param url
 * @param result
 * @returns {Promise<unknown>}
 */
export const remove = function(e, url, result) {
  return new Promise(resolve => {
    e.$confirm('此操作将会删除数据, 是否继续?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      exDelete(url, result)
        .then(res => {
          resolve(res)
        })
    }).catch(() => {
      resolve({ code: 100 })
    })
  })
}

/**
 * 提示框执行
 * @param e
 * @param options
 * @param result
 * @param config
 * @returns {Promise<unknown>}
 */
export const confirmExecute = function(e, options, result, config) {
  return new Promise(resolve => {
    config = Object.assign({}, {
      title: '提示',
      content: '该操作影响的数据将不可恢复, 是否继续?',
      sure: '确定',
      cancel: '取消',
      type: 'warning'
    }, config || {})

    e.$confirm(config.content, config.title, {
      confirmButtonText: config.sure,
      cancelButtonText: config.cancel,
      type: config.type
    }).then(() => {
      execute(options, result)
        .then(res => {
          resolve(res)
        }).catch(res => {
          resolve(res)
        })
    }).catch(() => {
      resolve({ code: 100 })
    })
  })
}

/**
 * 请求执行
 * @param options
 * @param result
 * @returns {Promise<unknown>}
 */
export const execute = function(options, result) {
  return new Promise(resolve => {
    store.dispatch('userInfos/refresh_token')
      .then(res => {
        if (res.code === 200) {
          service(options).then(res => {
            if (util.isEmpty(result)) {
              resolve(res)
            } else {
              resolve(util.callPropValue(res, result))
            }
          }).catch(res => {
            resolve(res)
          })
        } else {
          resolve(res)
        }
      }).catch(() => {
        resolve({ code: 401 })
      })
  })
}

/**
 * 消息提示
 * @param e
 * @param res
 * @param errorMsg 错误消息
 * @param hide
 */
export const resultMsg = function(e, res, errorMsg, hide) {
  if (res.code === 100) {
    return
  }
  let hideMsg = false
  if (util.isBoolean(errorMsg)) {
    hideMsg = errorMsg
  } else {
    hideMsg = util.toBoolean(hide)
    if (errorMsg) {
      res.msg = errorMsg
    }
  }
  if (res.code !== 200) {
    e.$message({
      showClose: true,
      type: 'error',
      message: res.msg || '操作失败!'
    })
  } else if (!hideMsg) {
    e.$message({
      showClose: true,
      type: 'success',
      message: res.msg || '操作成功!'
    })
  }
}
